#include "cadrepunishtab.h"
#include "ui_cadrepunishtab.h"
#include <QDialog>
#include <QGridLayout>
#include <QLineEdit>
#include <QSqlQuery>
#include <QPushButton>
#include <QSqlTableModel>
#include "timeeditdelegate.h"
#include <QDebug>
#include <QSqlRelationalTableModel>
#include <QSqlRelationalDelegate>
#include <QLabel>
#include <QHash>
#include <QDialogButtonBox>
#include <QMessageBox>
#include <QSqlError>
#include "cdateedit.h"
#include "cadreutil.h"

CadrePunishTab::CadrePunishTab(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::CadrePunishTab)
{
    ui->setupUi(this);
}

CadrePunishTab::~CadrePunishTab()
{
    delete ui;
}
void CadrePunishTab::initPunishTableView(QString curPerson)
{
    punishModel = new QSqlRelationalTableModel(this);
    punishModel->setTable("Punish");
    punishModel->setRelation(2, QSqlRelation("PunishTypeContrast", "PunishTypeID", "PunishType"));
    punishModel->setJoinMode(QSqlRelationalTableModel::LeftJoin);
    punishModel->setEditStrategy(QSqlTableModel::OnFieldChange);// OnRowChange OnFieldChange OnManualSubmit
    QString filter = QString::fromUtf8("IDNum='%1'").arg(curPerson);
    currentPerson=curPerson;
    punishModel->setFilter(filter);
    punishModel->select();

    punishModel->setHeaderData(1, Qt::Horizontal, QString::fromUtf8("惩处时间"));
    punishModel->setHeaderData(2, Qt::Horizontal, QString::fromUtf8("惩处类型"));
    punishModel->setHeaderData(3, Qt::Horizontal, QString::fromUtf8("宣布机关"));
    punishModel->setHeaderData(4, Qt::Horizontal, QString::fromUtf8("受惩戒名称"));
    punishModel->setHeaderData(5, Qt::Horizontal, QString::fromUtf8("受惩戒原因"));
    ui->punishTableView->horizontalHeader()->setVisible(true); //UI界面设置，总是恢复为false
    ui->punishTableView->setModel(punishModel);
    ui->punishTableView->setItemDelegate(new QSqlRelationalDelegate(ui->punishTableView));
    TimeEditDelegate *punishtime = new TimeEditDelegate();
    ui->punishTableView->setItemDelegateForColumn(1,punishtime);
    ui->punishTableView->setColumnHidden(0,true);
    ui->punishTableView->setColumnHidden(6,true);
    ui->punishTableView->setColumnWidth(1,130);
    ui->punishTableView->setColumnWidth(2,130);
    ui->punishTableView->setColumnWidth(3,150);
    ui->punishTableView->setColumnWidth(4,150);
    ui->punishTableView->setColumnWidth(5,150);
    ui->punishTableView->selectRow(punishModel->rowCount()-1);
}
void CadrePunishTab::setButtonEnable(bool i)
{
    ui->punishAddButton->setEnabled(i);
    ui->punishDeleteButton->setEnabled(i);
    ui->punishDownButton->setEnabled(i);
    ui->punishUpButton->setEnabled(i);
    if(i){
        ui->punishTableView->setEditTriggers(QAbstractItemView::DoubleClicked);
    }else{
        ui->punishTableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
    }

}

void CadrePunishTab::on_punishAddButton_clicked()
{
    QDialog *addDialog = new QDialog(this);
    addDialog->setWindowTitle(trUtf8("添加惩处信息"));
    addDialog->setGeometry(700,300,400,400);
    addDialog->setContentsMargins(10,0,10,0);
    QGridLayout *gridLayout = new QGridLayout(addDialog);
    gridLayout->setMargin(10);
    QLabel *timeLabel =new QLabel(trUtf8("惩处时间"),addDialog);
    QLabel *typeLabel=new QLabel(trUtf8("惩处类型"),addDialog);
    QLabel *declareagenLabel=new QLabel(trUtf8("宣布机关"),addDialog);
    QLabel *nameLabel=new QLabel(trUtf8("受惩戒名称"),addDialog);
    QLabel *reasonLabel=new QLabel(trUtf8("受惩戒原因"),addDialog);

    CDateEdit *time = new CDateEdit(addDialog);
    time->setNullable(true);
    time->setDisplayFormat("yyyy-MM-dd");
    time->setCalendarPopup(true);
    time->setDate(QDate());
    QComboBox *type = new QComboBox(addDialog);
    phash =CadreUtil::initComboBox(type,"","PunishTypeContrast", "PunishTypeID", "PunishType");
    QLineEdit *declareagen = new QLineEdit(addDialog);
    QLineEdit *name = new QLineEdit(addDialog);
    QLineEdit *reason = new QLineEdit(addDialog);
    gridLayout->addWidget(timeLabel,0,0);
    gridLayout->addWidget(time,0,1);
    gridLayout->addWidget(typeLabel,1,0);
    gridLayout->addWidget(type,1,1);
    gridLayout->addWidget(declareagenLabel,2,0);
    gridLayout->addWidget(declareagen,2,1);
    gridLayout->addWidget(nameLabel,3,0);
    gridLayout->addWidget(name,3,1);
    gridLayout->addWidget(reasonLabel,4,0);
    gridLayout->addWidget(reason,4,1);

    QPushButton *yesButton = new QPushButton(trUtf8("确定"));
    QPushButton *cancelButton = new QPushButton(trUtf8("取消"));

    QDialogButtonBox *buttonBox = new QDialogButtonBox(Qt::Horizontal);
    buttonBox->addButton(yesButton, QDialogButtonBox::AcceptRole);
    buttonBox->addButton(cancelButton, QDialogButtonBox::RejectRole);

    gridLayout->addWidget(buttonBox,5,0,1,2,Qt::AlignHCenter);

    connect(buttonBox, SIGNAL(accepted()), addDialog, SLOT(accept()));
    connect(buttonBox, SIGNAL(rejected()), addDialog, SLOT(reject()));

    if(addDialog->exec()==QDialog::Accepted){
        QSqlQuery query;
        query.prepare("insert into Punish (`PunishTime`, `PunishTypeID`, `DeclareAgen`,"
                      " `PunishName`, `PunishReason`, `IDNum`)"
                      " values(:time, :type, :declareagen, :name, :reason, "
                      ":idnum )");
        query.bindValue(":time",time->date());
        query.bindValue(":type",(phash.key(type->currentText())).toInt());
//        query.bindValue(":type",phash.key(type->currentText()));
        query.bindValue(":declareagen",declareagen->text());
        query.bindValue(":name",name->text());
        query.bindValue(":reason",reason->text());
        query.bindValue(":idnum",currentPerson);
        if(query.exec()){
            QMessageBox::information(this,trUtf8("提示"),
               trUtf8("插入数据成功"),QMessageBox::Yes);
            initPunishTableView(currentPerson);
        }else{
            QString info = query.lastError().databaseText();
            QMessageBox::information(this,trUtf8("插入数据库错误"),
               info,QMessageBox::Yes);
        }

    }else{

    }
}


QSqlTableModel* CadrePunishTab::getOriginalModel()
{
    QSqlTableModel *originalpunishModel = new QSqlTableModel(this);
    originalpunishModel->setTable("Punish");
    QString filter = QString::fromUtf8("IDNum='%1'").arg(currentPerson);
    originalpunishModel->setFilter(filter);
    originalpunishModel->select();
    originalpunishModel->setEditStrategy(QSqlTableModel::OnFieldChange);
    return originalpunishModel;
}

void CadrePunishTab::on_punishDeleteButton_clicked()
{
    CadreUtil::deleteRow(punishModel, ui->punishTableView);
}

void CadrePunishTab::on_punishUpButton_clicked()
{
    QSqlTableModel *originalpunishModel = getOriginalModel();
    int cur = CadreUtil::moveUpOne(originalpunishModel,0,ui->punishTableView);
    initPunishTableView(currentPerson);
    ui->punishTableView->selectRow(cur);
}

void CadrePunishTab::on_punishDownButton_clicked()
{
    QSqlTableModel *originalpunishModel = getOriginalModel();
    int cur =CadreUtil::moveDownOne(originalpunishModel,0,ui->punishTableView);
    initPunishTableView(currentPerson);
    ui->punishTableView->selectRow(cur);
}
